package com.jinkaimen.goodcash.ui.fragment;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ht.baselibs.bean.OperatorBean;
import com.ht.baselibs.core.EventCenter;
import com.ht.baselibs.core.EventCode;
import com.ht.baselibs.glide.CropCircleTransformation;
import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseOperatorListFragment;
import com.ht.baselibs.utils.ActivityUtils;
import com.ht.baselibs.utils.SharedPreferencesUtils;
import com.ht.baselibs.utils.UIUtils;
import com.igexin.sdk.PushManager;
import com.jinkaimen.goodcash.R;
import com.jinkaimen.goodcash.bean.AccountBean;
import com.jinkaimen.goodcash.dao.AccountDao;
import com.jinkaimen.goodcash.http.ConsultInterceptor;
import com.jinkaimen.goodcash.manager.ApiManager;
import com.jinkaimen.goodcash.service.GetuiService;
import com.jinkaimen.goodcash.ui.activity.IdentificationShopStep1Activity;
import com.jinkaimen.goodcash.ui.activity.LoginActivity;
import com.jinkaimen.goodcash.ui.activity.NotificationSettingActivity;
import com.jinkaimen.goodcash.ui.activity.SaleManagerActivity;
import com.jinkaimen.goodcash.ui.activity.ShopManagerActivity;
import com.jinkaimen.goodcash.ui.dialog.TipsDialog;
import com.jinkaimen.goodcash.utils.AppUtils;

import de.greenrobot.event.EventBus;
import retrofit2.Call;
import retrofit2.Response;

public class SelfFragment extends BaseOperatorListFragment {

    private ImageView mAvatarView;
    private TextView mTelView;
    private TipsDialog mTipsDialog;

    @Override
    protected boolean hasEvent() {
        return true;
    }

    @Override
    protected void initTitleBar() {

    }

    @Override
    protected View getHeaderView() {
        View headerView = UIUtils.inflate(R.layout.header_list_self_frag);
        mAvatarView = (ImageView)headerView.findViewById(R.id.iv_header_list_self_frag_avatar);
        mTelView = (TextView)headerView.findViewById(R.id.tv_header_list_self_frag_tel);
        mAvatarView.setOnClickListener(this);
        mTelView.setOnClickListener(this);
        return headerView;
    }

    @Override
    protected void loadData() {
        changeOperaor();
    }

    @Override
    protected void onEvent(int eventCode, Object data) {
        switch (eventCode){
            case EventCode.EVENT_CODE_START_LOGIN:
            case EventCode.EVENT_CODE_AUTHOR_SUCCESS:
                UIUtils.post(new Runnable() {
                    @Override
                    public void run() {
                        mDataList.clear();
                        changeOperaor();
                        mOperatorAdapter.notifyDataSetChanged();
                    }
                });
                break;
        }
    }

    public void changeOperaor(){
        final AccountBean accountBean = AccountDao.getInstance().getAccount();

        Glide.with(mContext)//
                .load(accountBean.profile)//
                .placeholder(R.mipmap.default_avatar)//
                .error(R.mipmap.default_avatar)//
                .bitmapTransform(new CropCircleTransformation(mContext))//
                .into(mAvatarView);

        if (AppUtils.isLogin()){
            mTelView.setText(accountBean.cellphone);
        }

        boolean isSaleLeader = accountBean.role == 1;
        if (isSaleLeader){
            String status = "";
            boolean isNeedShow = true;
            if (accountBean.auStep != 4){
                status = "未提交资料";
            } else {
                if (accountBean.auStatus == 2){
                    status = "待审核";
                } else if (accountBean.auStatus == 1){//审核成功
                    status = "";
                    isNeedShow = false;
                } else if (accountBean.auStatus == 3){
                    status = "审核驳回";
                }
            }

            if (isNeedShow){
                add(R.mipmap.sign_self_frag, "店铺签约信息", status, new OperatorBean.OnItemClickListener() {
                    @Override
                    public void onClick(boolean isChecked) {
                        if (AppUtils.authorStep(mContext, accountBean.auStep)){
                            if (accountBean.auStatus == 2){
                                UIUtils.showToast("请等待审核结果");
                            } else if (accountBean.auStatus == 1){//审核成功
                                //TODO 到店铺签约信息界面
                            } else if (accountBean.auStatus == 3){
                                mTipsDialog = new TipsDialog(mContext);
                                mTipsDialog.setContent("是否重新签约信息");
                                mTipsDialog.show();
                                mTipsDialog.setOnDialogTipsClickListener(new TipsDialog.OnDialogTipsClickListener(){

                                    @Override
                                    public void doEnter() {
                                        mTipsDialog.dismiss();
                                        ActivityUtils.startActivity(mContext, IdentificationShopStep1Activity.class);
                                    }

                                    @Override
                                    public void doCancel() {
                                        mTipsDialog.dismiss();
                                    }
                                });
                            }
                        }
                    }
                });
            }

            add(R.mipmap.store_manager_self_frag, "店铺管理", new OperatorBean.OnItemClickListener() {
                @Override
                public void onClick(boolean isChecked) {
                    if (accountBean.auStatus == 1){
                        ActivityUtils.startActivity(mContext, ShopManagerActivity.class);
                    } else {
                        UIUtils.showToast("请等待审核");
                    }
                }
            });

            add(R.mipmap.clerk_self_frag, "店员管理", new OperatorBean.OnItemClickListener() {
                @Override
                public void onClick(boolean isChecked) {
                    if (accountBean.auStatus == 1) {
                        ActivityUtils.startActivity(mContext, SaleManagerActivity.class);
                    } else {
                        UIUtils.showToast("请等待审核");
                    }
                }
            });
        }

        add(isSaleLeader,R.mipmap.voice_setting_self_frag,  "收款提示音", new OperatorBean.OnItemClickListener() {
            @Override
            public void onClick(boolean isChecked) {
                ActivityUtils.startActivity(mContext, NotificationSettingActivity.class);
            }
        });


//        add(true, R.mipmap.settlement_self_frag, "结算设置", new OperatorBean.OnItemClickListener() {
//            @Override
//            public void onClick(boolean isChecked) {
//                UIUtils.showToast("店铺管理");
//            }
//        });

//        add(true, R.mipmap.setting_self_frag, "安全设置", new OperatorBean.OnItemClickListener() {
//            @Override
//            public void onClick(boolean isChecked) {
//                UIUtils.showToast("店铺管理");
//            }
//        });

        add(R.mipmap.exit_self_frag,"退出登录", new OperatorBean.OnItemClickListener() {
            @Override
            public void onClick(boolean isChecked) {
                mTipsDialog = new TipsDialog(mContext);
                mTipsDialog.setContent("是否退出登录");
                mTipsDialog.show();
                mTipsDialog.setOnDialogTipsClickListener(new TipsDialog.OnDialogTipsClickListener(){

                    @Override
                    public void doEnter() {
                        mTipsDialog.dismiss();
                        mContext.showLoadingDialogCanotCancle();
                        String token = SharedPreferencesUtils.getString(ConsultInterceptor.ACCESS_TOKEN, "");
                        ApiManager.getApi().logout(
                                "HSY001018",
                                token
                        ).enqueue(new NetCallBack<String>() {
                            @Override
                            protected void onSuccess(Call<String> call, Response<String> response) {
                                AppUtils.logout();
                                EventBus.getDefault().post(new EventCenter(EventCode.EVENT_CODE_BALANCE_CHANGE));
                                mAvatarView.setImageResource(R.mipmap.default_avatar);
                                EventBus.getDefault().post(new EventCenter(EventCode.EVENT_CODE_START_LOGOUT));
                                mListView.smoothScrollToPosition(0);
                                mTelView.setText("立即登陆");
                            }

                            @Override
                            protected void hiddenProgress() {
                                mContext.hideLodingDialog();
                            }

                            @Override
                            protected boolean isNullNeedCallback() {
                                return true;
                            }
                        });
                    }

                    @Override
                    public void doCancel() {
                        mTipsDialog.dismiss();
                    }
                });
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_header_list_self_frag_avatar:
            case R.id.tv_header_list_self_frag_tel:
                if (AppUtils.isLogin()){
                    AppUtils.authorStep(mContext, AccountDao.getInstance().getAccount().auStep);
                } else {
                    ActivityUtils.startActivity(mContext, LoginActivity.class);
                }
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        PushManager.getInstance().registerPushIntentService(mContext, GetuiService.class);
    }
}